<?php
namespace App\Http\Controllers;

use App\Exceptions\ValidateException;
use App\Model\Setting;
use App\Repository\Contracts\RefundShoppingInterface;
use App\Repository\Contracts\SettingInterface;
use App\Repository\Contracts\ShoppingInterface;
use App\Support\Helper\CommonHelper;
use Illuminate\Http\Request;
use Auth;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-17 20:15
 *
 * @Description: 购物
 */
class ShoppingController extends Controller
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var ShoppingInterface
     */
    protected $shopping;

    /**
     * @var RefundShoppingInterface
     */
    protected $refund;

    protected $uid;

    protected $storeId;

    /**
     * ShoppingController constructor.
     * @param Request $request
     * @param ShoppingInterface $shopping
     * @param RefundShoppingInterface $refund
     */
    public function __construct(Request $request, ShoppingInterface $shopping, RefundShoppingInterface $refund)
    {
        $this->request = $request;

        $this->shopping = $shopping;

        $this->refund = $refund;

        $this->uid = CommonHelper::getToken(Auth::user())['uid'];

        $this->storeId = CommonHelper::getToken(Auth::user())['storeId'];
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $limit = $this->request->input('limit', 15);
        $page = $this->request->input('page', 1);
        $tradeNo = $this->request->input('trade_no', null);
        //Search Map
        $searchMap = $this->searchMap([
            ['out_trade_no', $tradeNo, '=']
        ]);
        $list = $this->shopping->lists($limit, $page, $searchMap);

        return $this->success($list);
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id)
    {
        $uid = $this->request->input('member_id', null);
        $map = $this->searchMap([
            ['uid', $uid, '=']
        ]);
        $detail = $this->shopping->detail($map);

        return $this->success($detail);
    }

    /**
     * 根据购物号返回订单详情
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function findByNumber()
    {
        $tradeNo = $this->request->input('tradeNo', null);
        $map = $this->searchMap([
            ['out_trade_no', $tradeNo, '=']
        ]);
        $detail = $this->shopping->detail($map);

        return $this->success($detail);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function created()
    {
        try {
            $data['auth_code'] = $this->request->input('authCode', null);
            $data['out_trade_no'] = $this->request->input('outTradeNo', null);
            $data['trade_no'] = $this->request->input('tradeNo', null);
            $data['pay_type'] = $this->request->input('payType', 1);
            $data['card_amount'] = $this->request->input('cardAmount', null);
            $data['amount'] = $this->request->input('amount', null);
            $data['member_id'] = $this->request->input('uid', null);
            $data['admin_id'] = $this->uid;
            $data['store_id'] = $this->storeId;
            $id = $this->shopping->created($data);

            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * 同步
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function sync()
    {
        try {
            $storeId = $this->storeId;
            $id = $this->shopping->sync($storeId);

            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * 同步
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function syncAmount()
    {
        try {
            $storeId = $this->storeId;
            $id = $this->shopping->syncAmount($storeId);

            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * 退单
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function refund()
    {
        try {
            $data['trade_no'] = $this->request->input('tradeNo');
            $data['amount'] = $this->request->input('amount');
            $goodsIds = $this->request->input('goodsIds');
            $counts = $this->request->input('counts');
            $goodsIds = explode(',', $goodsIds);
            $counts = explode(',', $counts);
            if ((!is_array($goodsIds) && count($goodsIds) == 0) || count($goodsIds) !== count($counts)) {
                return $this->fail('参数错误');
            }
            $data['source'] = [];
            foreach ($goodsIds as $key => $val) {
                $data['source'][] = ['goods_id' => $val, 'counts' => $counts[$key]];
            }
            $data['admin_id'] = $this->uid;
            $data['store_id'] = $this->storeId;
            $id = $this->refund->created($data);

            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function refundList()
    {
        $limit = $this->request->input('limit', 15);
        $page = $this->request->input('page', 1);
        $tradeNo = $this->request->input('trade_no', null);
        //Search Map
        $searchMap = $this->searchMap([
            ['out_trade_no', $tradeNo, '=']
        ]);
        $list = $this->refund->lists($limit, $page, $searchMap);

        return $this->success($list);
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function refundDetail($id)
    {
        $uid = $this->request->input('member_id', null);
        $map = $this->searchMap([
            ['uid', $uid, '='],
            ['id', $id, '=']
        ]);
        $detail = $this->refund->detail($map);

        return $this->success($detail);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function syncRefund()
    {
        try {
            $storeId = $this->storeId;
            $id = $this->refund->sync($storeId);

            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function syncAmountRefund()
    {
        try {
            $storeId = $this->storeId;
            $id = $this->refund->syncAmount($storeId);

            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function ticket()
    {
        $outTradeNo = $this->request->input('outTradeNo', null);
        $ticketType = $this->request->input('ticketType', 1);
        $map = $this->searchMap([
            ['out_trade_no', $outTradeNo, '=']
        ]);

        $detail = $ticketType == 1 ? $this->shopping->detail($map) : $this->refund->detail($map);
        $detail['storeConfig'] = json_decode(app(SettingInterface::class)->findByName(Setting::STORE_CONFIG)['config'], true);

        return $this->success($detail);
    }
}